package org.zjt.design.pattern;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Observable;
import java.util.Observer;

/**
 * @author <a href="juntao.zhang@qq.com">juntao.zhang</a>
 * @Description:
 * @Package org.zjt.design.pattern
 * @date 2018/5/8 17:46
 * @see
 */
public class ObserverTest {
    private static Logger log = LoggerFactory.getLogger(ObserverTest.class);

    public static void main(String[] args) {


        ObservableA observableA = new ObservableA();
        observableA.addObserver(new ObserverB());
        observableA.action("zhangsan");
    }

}

/**
 * 被观察对象
 */
@Data
class ObservableA extends Observable{
    private static Logger log = LoggerFactory.getLogger(ObserverTest.class);

    public void action(String msg){
        log.info(msg);
        setChanged();
        notifyObservers(msg);
    }

}


/**
 * 观察者
 */
@Slf4j
class ObserverB implements Observer {

    @Override
    public void update(Observable o, Object arg) {
        log.info(String.valueOf(o) + "   " + arg);
    }
}